package cn.compiler;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestCompiler {
    public static void main(String args[]) throws IOException {
        //动态编译
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result=compiler.run(null,null,null,"D:/myjava/HelloWord.java");
        System.out.println(result==0?"编译成功":"编译失败");
        Runtime run = Runtime.getRuntime();
        Process process = run.exec("java -cp D:/ HelloWord");
        InputStream in= process.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
        String read="";
        while ((read=bufferedReader.readLine())!=null){

            System.out.print(read);
        }
    }
}
